//
//  PHomeController.swift
//  CJPack
//
//  Created by 陈警卫 on 2023-04-21.
//

import UIKit
import SwiftUI


class PHomeController: CJBaseMenuController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.setupView()
        self.setupData()
        
        
        let data: [UInt8] = [0x12, 0x34, 0x56]

        let allTypes: [CJCRC8Type] = [
            .autosar,
            .bluetooth,
            .cdma2000,
            .darc,
            .dvbS2,
            .gsmA,
            .gsmB,
            .hitag,
            .i432_1,
            .iCode,
            .lte,
            .maximDow,
            .mifareMad,
            .nrsc5,
            .opensafety,
            .rohc,
            .saeJ1850,
            .smbus,
            .tech3250,
            .wcdma
        ]

        for type in allTypes {
            let res = CJCRCHub.computeCRC8(data, type: type)
            let hex = String(format: "%02X", res)
            print("\(type): 0x\(hex)")
        }
        
        let allTypes16: [CJCRC16Type] = [
            .arc,
            .cdma2000,
            .cms,
            .dds110,
            .dectR,
            .dectX,
            .dnp,
            .en13757,
            .genibus,
            .gsm,
            .ibm3740,
            .ibmSdlc,
            .isoIec144433A,
            .kermit,
            .lj1200,
            .m17,
            .maximDow,
            .mcrf4xx,
            .modbus,
            .nrsc5,
            .opensafetyA,
            .opensafetyB,
            .profibus,
            .riello,
            .spiFujitsu,
            .t10Dif,
            .teledisk,
            .tms37157,
            .umts,
            .usb,
            .xmodem
        ]

        for type in allTypes16 {
            let result = CJCRCHub.computeCRC16(data, type: type)
            let hex = String(format: "%04X", result)
            print("\(type): 0x\(hex)")
        }
        
        let allTypes32: [CJCRC32Type] = [
            .aixm,       // CRC-32Q
            .autosar,
            .base91D,    // CRC-32D
            .bzip2,      // CRC-32B
            .cdRomEdc,
            .cksum,      // POSIX
            .iscsi,      // Castagnoli
            .isoHdlc,    // PKZip/V42/ADDC
            .jamcrc,
            .mef,
            .mpeg2,
            .xfer,
            .sata
        ]

        for type in allTypes32 {
            let result = CJCRCHub.computeCRC32(data, type: type)
            let hex = String(format: "%08X", result)
            print("\(type): 0x\(hex)")
        }



        print("")
        
    }
    
    var tableView: UITableView!
    var datas: [String] = []
    
    func setupView() {
        
        self.tableView = UITableView(frame: CGRect(x: 0, y: CJScreen.navBatBarHeight, width: self.view.cj_width, height: self.view.cj_height - CJScreen.navBatBarHeight), style: .plain)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.separatorStyle = .none
        self.tableView.backgroundColor = .clear
        self.view.addSubview(self.tableView)
        
        self.tableView.register(PHomeCell.self, forCellReuseIdentifier: "PHomeCell")
    }
    
    func setupData() {
        
        self.datas = ["ex.log", "分享", "CateTest", "HttpTest", "Charts", "测试", "相册", "扫描", "多层布局", "菜单视图", "视频播放", "弹窗", "SwiftUI", "文件处理", "加载", "Popver"]
        self.tableView.reloadData()
        
    }
}

extension PHomeController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PHomeCell", for: indexPath)
        cell.selectionStyle = .none
        if let cell = cell as? PHomeCell {
            cell.param = self.datas[indexPath.item]
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let title = self.datas[indexPath.item]
        
        if title == "ex.log" {
            let vc = CELogController()
            self.navigationController?.pushViewController(vc, animated: true)
        } else if title == "分享" {
            let vc = PShareController()
            self.navigationController?.pushViewController(vc, animated: true)
        } else if title == "CateTest" {
            cateAction()
        } else if title == "HttpTest" {
            httpAction()
        }
        else if title == "Charts" {
            chartsAction()
        } else if title == "相册" {
            CJPhotoPicker().show()
        }  else if title == "扫描" {
            let vc = CJPScanController()
            self.navigationController?.pushViewController(vc, animated: true)
        } else if title == "多层布局" {
            let vc = PMenuController()
            self.navigationController?.pushViewController(vc, animated: true)
        } else if title == "菜单视图" {
            let vc = CJMenuController()
            self.navigationController?.pushViewController(vc, animated: true)
        } else if title == "视频播放" {
            let vc = PPlayController()
            self.navigationController?.pushViewController(vc, animated: true)
        } else if title == "弹窗" {
            let vc = CJPopupController()
            self.navigationController?.pushViewController(vc, animated: true)
        } else if title == "SwiftUI" {
            jumpSwiftUI()
        } else if title == "文件处理" {
            jumpFile()
        } else if title == "加载" {
            jumpHud()
        } else if title == "Popver" {
            let vc = CJPopoverController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}

extension PHomeController {
    
    func cateAction() {
        navigationController?.pushViewController(CECateController(), animated: true)
    }
    
    func httpAction() {
        navigationController?.pushViewController(CEHttpController(), animated: true)
    }
    
    
    
    
    
    
    
    func chartsAction() {
        let vc = PChartsController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    func jumpSwiftUI() {
        let vc = PFirstC()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func jumpFile() {
        let vc = PFileController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func jumpHud() {
        let vc = CJHudController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
}


class PHomeCell: UITableViewCell {
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = .clear
        self.setupView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var backV: UIView!
    var titleL: UILabel!
    
    var param: String? {
        didSet {
            self.titleL.text = self.param ?? ""
        }
    }
    
    func setupView() {
        self.backV = UIView()
        self.addSubview(self.backV)
        
        self.titleL = UILabel()
        self.backV.addSubview(self.titleL)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.backV.frame = CGRect(x: 0, y: 0, width: self.cj_width, height: self.cj_height)
        self.titleL.frame = CGRect(x: 20, y: 0, width: self.backV.cj_width - 40, height: 30)
        self.titleL.cj_centerY = self.backV.cj_height * 0.5
    }
}
